//==============================================================================
// ************************* 78 character ruler *****************************
//
// Title:       BIST_CheckLocalI2cBus.c
// Purpose:     A short description of the implementation.
//
// Created on:  2013-3-1 at 15:00:35 by Vinson.Zhang
// Copyright:   dafda. All Rights Reserved.
//
// (c) 2008 Nokia Siemens Networks.
// ************************* 78 character ruler *****************************

//==============================================================================
// Include files

#include "RFBB.h"         
#include <ansi_c.h>
#include <utility.h>
#include <cvirte.h>     
#include <userint.h>
#include <formatio.h>
#include "USB6501.h"
//==============================================================================
// Constants

//==============================================================================
// Global variables

_SHMON_GET_I2C_TEMP_CMD g_cFRMONGetI2cTempCmd[17] = {{"FRMon,action,load,agent,TemperatureSensorAccess,Vegas,_TemperatureSensorAccess","done",0.0},
													  	 {"FRMon/Vegas/_TemperatureSensorAccess,action,init,TemperatureSensorAccess,","done",0.0},
														 {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,BOARD1","+",0.0},
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,CORE1","+",0.0}, 
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,RF1","+",0.0},
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,BOARD2","+",0.0},
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,CORE2","+",0.0},
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,RF2","+",0.0},
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,BOARD3","+",0.0},
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,CORE3","+",0.0},
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,RF3","+",0.0},
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,BOARD4","+",0.0},
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,CORE4","+",0.0},
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,RF4","+",0.0},
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,Murkku2BOARD","+",0.0},
                                                         {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,Murkku2CORE","+",0.0},
														 {"FRMon/Vegas/_TemperatureSensorAccess,action,terminate,TemperatureSensorAccess","done",0.0}};
														 

_SHMON_GET_I2C_TEMP_CMD g_cFRMONGetPAI2cTempCmd[5] = {{"FRMon,action,load,agent,TemperatureSensorAccess,Vegas,_TemperatureSensorAccess","done",0.0},
													  	 {"FRMon/Vegas/_TemperatureSensorAccess,action,init,TemperatureSensorAccess,","done",0.0},
														 {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,PA1","+",0.0},
														 {"FRMon/Vegas/_TemperatureSensorAccess,action,getTemperature,PA3","+",0.0},
														 {"FRMon/Vegas/_TemperatureSensorAccess,action,terminate,TemperatureSensorAccess","done",0.0}};

/**************************************************************************************************
  Function:         BIST_CheckLocalI2cBus
  Description:      This test aims to check that temperature sensors are undamaged.
  Calls:            none
  Called By:        dll
  Table Accessed:   none
  Table Updated:    none
  Input:            none
  Output:           error code
  Return:           none
  Test Case ID:     FRHD_LocalI2C
  Used Command:     g_cFRMONGetI2cTempCmd
**************************************************************************************************/
void __decldllexp BIST_CheckLocalI2cBus(tTestData *testData, tTestError *testError)
{
    
    
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    TSObj_Property stepNumericArrayHandle = 0; 
    char* pszStepName = NULL;    
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};
    char cChannel[BUFFER_SIZE_256] ={'\0'};
    int iPipeNum = 0;
    int iTempType = 0;
    int iPassFail = 0;
	int iReturn = 0;
	int error = 0;
    int iArryNum = 0;
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));
   
    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );
   
    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));
    
    TS_PropertyGetPropertyObject( testData->seqContextCVI, &errorInfo,"Step.NumericArray", 0, 
                                          &stepNumericArrayHandle );  
    
     //InetTelnetOpen(FRCommand) 
	 g_hFrmonAgent = RfbbInetTelnetOpen (g_szDUTIP, g_iPort, 0);
	  for(iArryNum = 0;iArryNum < 2;iArryNum++)
      	{   //send FRMon and init 
            sprintf(kszShmonAgent,g_cFRMONGetI2cTempCmd[iArryNum].cGetI2cTempCmd);    
			FRmonAgent_String(kszShmonAgent, -1,g_cFRMONGetI2cTempCmd[iArryNum].cSearchPattern, &iPassFail); 
        	if (iPassFail!=1) 
        		{
            goto Error;
        		}
      	}
	  for(iArryNum = 2;iArryNum < 16;iArryNum++) 
	  	{   //send FRMon and measure temperature   
		  sprintf(kszShmonAgent,g_cFRMONGetI2cTempCmd[iArryNum].cGetI2cTempCmd);
          ErrChk(FRmonAgent_Numeric(kszShmonAgent, -1, g_cFRMONGetI2cTempCmd[iArryNum].cSearchPattern, &g_cFRMONGetI2cTempCmd[iArryNum].dReturnTemp)); 
	  	}
	 
	
Error:
	 //action terminate
    sprintf(kszShmonAgent,g_cFRMONGetI2cTempCmd[16].cGetI2cTempCmd);
    Frmon_RunCmdNoAck(kszShmonAgent, 1);

#if(ON_OFF != SIMU_VERS)

    //output I2C temperature values
    for(iArryNum = 2;iArryNum < 16;iArryNum++)
    	{   
            TS_PropertySetValNumberByOffset( stepNumericArrayHandle, &errorInfo,iArryNum-2, 0, g_cFRMONGetI2cTempCmd[iArryNum].dReturnTemp);
    	} 
	
	//InetTelnetClose(FRCommand);
	InetTelnetClose(g_hFrmonAgent);  
	
	//FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
	CA_DISCARD_OBJ( stepNumericArrayHandle );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if (error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }

#else
    //output I2C temperature values
    for(iPipeNum = 0;iPipeNum < PIPE_MAXNUM;iPipeNum++)
    {        
        for(iTempType = 0;iTempType < 3;iTempType++)
        {
            iArryNum = iPipeNum * 3 + iTempType;
            TS_PropertySetValNumberByOffset( stepNumericArrayHandle, &errorInfo,iArryNum, 0, g_cFRMONGetI2cTempCmd[iArryNum].dReturnTemp);
        }
    }


#endif
    // Clear message from GUI
    TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

    return;    
}

/**************************************************************************************************
  Function:         BIST_CheckPAI2cBus
  Description:      This test aims to check that PA temperature sensors are undamaged.
  Calls:            none
  Called By:        dll
  Table Accessed:   none
  Table Updated:    none
  Input:            none
  Output:           error code
  Return:           none
  Test Case ID:     FRHD_PAI2C
  Used Command:     g_cFRMONGetPAI2cTempCmd
**************************************************************************************************/
void __decldllexp BIST_CheckPAI2cBus(tTestData *testData, tTestError *testError)
{
    
    
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    TSObj_Property stepNumericArrayHandle = 0; 
    char* pszStepName = NULL;    
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};
    char cChannel[BUFFER_SIZE_256] ={'\0'};
    int iPipeNum = 0;
    int iTempType = 0;
    int iPassFail = 0;
	int iReturn = 0;
	int error = 0;
    int iArryNum = 0;
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));
   
    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );
   
    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));
    
    TS_PropertyGetPropertyObject( testData->seqContextCVI, &errorInfo,"Step.NumericArray", 0, 
                                          &stepNumericArrayHandle );  
    
	//InetTelnetOpen(FRCommand) 
	 g_hFrmonAgent = RfbbInetTelnetOpen (g_szDUTIP, g_iPort, 0);
	 
	  for(iArryNum = 0;iArryNum < 2;iArryNum++)
      	{   //send FRMon and init 
            sprintf(kszShmonAgent,g_cFRMONGetPAI2cTempCmd[iArryNum].cGetI2cTempCmd);    
			FRmonAgent_String(kszShmonAgent, -1,g_cFRMONGetPAI2cTempCmd[iArryNum].cSearchPattern, &iPassFail); 
        	if (iPassFail!=1) 
        		{
            goto Error;
        		}
      	}
	  for(iArryNum = 2;iArryNum < 4;iArryNum++) 
	  	{   //send FRMon and measure temperature   
		  sprintf(kszShmonAgent,g_cFRMONGetPAI2cTempCmd[iArryNum].cGetI2cTempCmd);
          ErrChk(FRmonAgent_Numeric(kszShmonAgent, -1, g_cFRMONGetPAI2cTempCmd[iArryNum].cSearchPattern, &g_cFRMONGetPAI2cTempCmd[iArryNum].dReturnTemp)); 
	  	}
	
Error:
	   //action terminate
    sprintf(kszShmonAgent,g_cFRMONGetPAI2cTempCmd[4].cGetI2cTempCmd);
    Frmon_RunCmdNoAck(kszShmonAgent, 1);

#if(ON_OFF != SIMU_VERS)

    TS_PropertySetValNumberByOffset( stepNumericArrayHandle, &errorInfo,0, 0, g_cFRMONGetPAI2cTempCmd[2].dReturnTemp);
    TS_PropertySetValNumberByOffset( stepNumericArrayHandle, &errorInfo,1, 0, g_cFRMONGetPAI2cTempCmd[3].dReturnTemp);
    
	//InetTelnetClose(FRCommand)
    InetTelnetClose(g_hFrmonAgent);
	
    //FREE RESOURCES    
    CA_DISCARD_OBJ( hStepName );  	
	CA_DISCARD_OBJ( stepNumericArrayHandle );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if (error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }

#else

#endif
    // Clear message from GUI
    TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

    return;    
}

